
class RewardView extends BaseView{


    private hasSet:boolean = false;
    private personalTab:TabItem = null;
    private gangTab:TabItem = null;
    constructor(){
        super();
        this.initView();
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.addToStage,this);
    }

    private addToStage():void{
        if(GameData.personalRank ){
            if(!this.hasSet){
                this.hasSet = true;
                this.setContentView(GameData.personalRank,GameData.gangRank);
            }
            this.addAnimation();
        }else {
            Main.removeRewardView();
        };
    }



    private containerView:egret.Sprite = null;
    private contentView:egret.Sprite = null;
    private scroller:egret.ScrollView = null;
    private gangScroller:egret.ScrollView = null;
    //窗口最大高度
    private maxHeight:number = GameData.stageH * 0.8;


    private initView():void{
        let bgMask:egret.Shape = new egret.Shape();
        bgMask.width = this.width;
        bgMask.height = this.height;
        bgMask.graphics.beginFill(0x000000,0.6);
        bgMask.graphics.drawRect(0,0,this.width,this.height);
        bgMask.graphics.endFill();
        this.addChild(bgMask);
        bgMask.touchEnabled = true;



    }



    private setContentView(data:any,data2:any):void{
        data.sort(function (item1:any,item2:any) {
            if(item1.rank > item2.rank){
                return 1;
            }
            return -1;
        });
        data2.sort(function (item1:any,item2:any) {
            if(item1.rank > item2.rank){
                return 1;
            }
            return -1;
        });

        this.containerView = utils.createSprite({
            width:GameData.stageW*0.77,
            height:GameData.stageH * 0.7,
            offsetX:0.5,
            offsetY:0.5,
            x:GameData.stageW/2,
            y:GameData.stageH/2
        });
        this.addChild(this.containerView);
        let bg:egret.Bitmap = utils.createBitmapByName('jldj_bg');
        bg.width = this.containerView.width;
        bg.height = this.containerView.height;
        this.containerView.addChild(bg);

        let title:egret.Bitmap = utils.createBitmapByName('jldjpng');
        title.x = this.containerView.width/2 - title.width/2;
        title.y = - title.height/2;
        this.containerView.addChild(title);


        let tab1:TabItem = new TabItem({
            content:'个人奖励'
        });
        tab1.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changePersonal,this);
        tab1.changeSelected(true);
        this.personalTab = tab1;
        let tab2:TabItem = new TabItem({
            content:'帮会奖励'
        });
        tab2.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGang,this);
        this.gangTab = tab2;

        tab1.x = this.containerView.width/2 - (tab1.width + tab2.width + 50)/2;
        tab1.y = title.height*0.7;
        this.containerView.addChild(tab1);
        tab2.x = tab1.x + tab1.width + 50;
        tab2.y = title.height*0.7;
        this.containerView.addChild(tab2);


        let scroller:egret.ScrollView = new egret.ScrollView();
        scroller.width = this.containerView.width;
        scroller.height = this.containerView.height - title.height - tab1.height - 10;
        scroller.y = this.containerView.height - scroller.height -20;
        this.containerView.addChild(scroller);
        this.scroller = scroller;

        let scroller2:egret.ScrollView = new egret.ScrollView();
        scroller2.width = this.containerView.width;
        scroller2.height = this.containerView.height - title.height - tab1.height - 10;
        scroller2.y = this.containerView.height - scroller2.height -20;
        this.containerView.addChild(scroller2);
        this.gangScroller = scroller2;
        this.gangScroller.visible = false;

        this.contentView = utils.createSprite({
            width:scroller.width
        });
        scroller.setContent(this.contentView);

        let gangView = utils.createSprite({
            width:scroller2.width
        });
        scroller2.setContent(gangView);

        let totalY = 0;
        for(var index = 0 ; index < data.length  ; index++){
            let rankItem  = new RankItem(index+1);
            rankItem.x = 10;
            rankItem.y = totalY;
            totalY += rankItem.height;
            this.contentView.addChild(rankItem);
            var list = data[index].props;
            for(var i = 0 ; i < list.length; i++){
                let item = new RewardItem(list[i]);
                item.x = this.contentView.width/2 - item.width/2;
                item.y = totalY;
                totalY += item.height;
                this.contentView.addChild(item);
            }
        }
        this.contentView.graphics.beginFill(0x000000,0);
        this.contentView.graphics.drawRect(0,0,this.contentView.width,this.contentView.height);
        this.contentView.graphics.endFill();


        let totalY2 = 0;
        for(var index = 0 ; index < data2.length ; index++){
            let rankItem  = new RankItem(index+1);
            rankItem.x = 10;
            rankItem.y = totalY2;
            totalY2 += rankItem.height;
            gangView.addChild(rankItem);
            var list = data2[index].props;
            for(var i = 0 ; i < list.length; i++){
                let item = new RewardItem(list[i]);
                item.x = gangView.width/2 - item.width/2;
                item.y = totalY2;
                totalY2 += item.height;
                gangView.addChild(item);
            }
        }
        gangView.graphics.beginFill(0x000000,0);
        gangView.graphics.drawRect(0,0,gangView.width,gangView.height);
        gangView.graphics.endFill();



        let btn_close:egret.Bitmap = utils.createBitmapByName('x',{offsetX:0.5,offsetY:0.5});
        btn_close.x = this.containerView.width - 5;
        btn_close.y = 5;
        btn_close.touchEnabled = true;
        btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP,this.remove,this);
        this.containerView.addChild(btn_close);


    }

    // 添加动画
    private addAnimation(){
        this.containerView.scaleX =  0.1;
        this.containerView.scaleY =  0.1;
        egret.Tween.removeTweens(this.containerView);
        egret.Tween.get(this.containerView).to({scaleX:1, scaleY:1}, 500, egret.Ease.backOut);
    }

    private remove():void{
        Main.removeRewardView();
    }


    private changePersonal():void{
        if(this.personalTab.isSelected){
            return;
        }
        this.personalTab.changeSelected(true);
        this.gangTab.changeSelected(false);
        this.scroller.visible = true;
        this.gangScroller.visible = false;
    }

    private changeGang():void{
        if(this.gangTab.isSelected){
            return;
        }
        this.personalTab.changeSelected(false);
        this.gangTab.changeSelected(true);
        this.scroller.visible = false;
        this.gangScroller.visible = true;
    }
}